package com.retailshop.shopfinace.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.retailshop.shopfinace.common.*;
import com.retailshop.shopfinace.entity.*;
import com.retailshop.shopfinace.service.FinaceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/Finace")
public class FinaceController {

    @Autowired
    FinaceService finaceService;

    /**
     * 读取所有数据
     *
     * @param request
     * @param jsonParam
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/List", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public Response AdminList(HttpServletRequest request, @RequestBody JSONObject jsonParam) {

        Map<String, Object> map = FinaceVer.Security(request, jsonParam);
        if (!map.get("code").toString().equals("000000")) {
            return new Response(MessageConstants.MSG_ACCESS_DENY, null, "接口参数错误", request);
        } else {
            try {
                String res = map.get("parm").toString();

                int AID = Integer.parseInt(map.get("AID").toString());
                System.out.println("AID:" + AID);
                //将接收到的数据转化为对象
                Finace finace = JSON.parseObject(res, new TypeReference<Finace>() {
                });

                System.out.println("分类：" + finace.getClassify());
                System.out.println("编号：" + finace.getUserNo());

                PageHelper.startPage(finace.getPage(), finace.getLimit());

                //读取数据表
                List<Finace> listFinace = finaceService.selectFinaceList(finace);
                if (listFinace != null) {

                    PageInfo<Finace> pageInfo = new PageInfo<>(listFinace);
                    int count = (int) pageInfo.getTotal();

                    return new Response(MessageConstants.MSG_SUCCESS, pageInfo.getList(), count, "", request);
                } else {
                    return new Response(MessageConstants.MSG_SUCCESS, null, "没有数据", request);
                }
            } catch (Exception ex) {
                return new Response(MessageConstants.MSG_ACCESS_DENY, ex.getMessage(), 0, "读取数据错误", request);
            }
        }
    }

    /**
     * 添加数据
     *
     * @param request
     * @param jsonParam
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/Add", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public Response AdminAdd(HttpServletRequest request, @RequestBody JSONObject jsonParam) {
        Map<String, Object> map = FinaceVer.Security(request, jsonParam);
        if (!map.get("code").toString().equals("000000")) {
            return new Response(MessageConstants.MSG_ACCESS_DENY, null, "接口参数错误", request);
        } else {
            try {
                String res = map.get("parm").toString();
                int AID = Integer.parseInt(map.get("AID").toString());
                System.out.println("AID:" + AID);

                //将接收到的数据转化为对象
                Finace finace = JSON.parseObject(res, new TypeReference<Finace>() {
                });

                //System.out.println("YYY");
                if(finace.getState()==null)
                {
                    finace.setState(true);
                }
                int row = finaceService.insertFinace(finace);
                //System.out.println("row:"+row);
                if (row > 0) {
                    return new Response(MessageConstants.MSG_SUCCESS, finace, row, "", request);
                } else {
                    return new Response(MessageConstants.MSG_ITEM_NOT_EXIST, null, 0, "添加数据错误", request);
                }
            } catch (Exception ex) {
                return new Response(MessageConstants.MSG_ACCESS_DENY, ex.getMessage(), 0, "添加数据错误", request);
            }
        }
    }


    /**
     * 查询数据
     *
     * @param request
     * @param jsonParam
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/SelfBalance", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public Response SelfBalance(HttpServletRequest request, @RequestBody JSONObject jsonParam) {
        Map<String, Object> map = FinaceVer.Security(request, jsonParam);
        if (!map.get("code").toString().equals("000000")) {
            return new Response(MessageConstants.MSG_ACCESS_DENY, null, "接口参数错误", request);
        } else {
            try {
                String res = map.get("parm").toString();
                int AID = Integer.parseInt(map.get("AID").toString());
                System.out.println("AID:" + AID);

                //将接收到的数据转化为对象
                FinaceBalance finaceBalance1 = JSON.parseObject(res, new TypeReference<FinaceBalance>() {
                });

                FinaceBalance finaceBalance = finaceService.SelectBalance(finaceBalance1);

                if (finaceBalance != null) {
                    return new Response(MessageConstants.MSG_SUCCESS, finaceBalance, 1, "", request);
                } else {
                    return new Response(MessageConstants.MSG_ITEM_NOT_EXIST, null, 0, "读取数据错误", request);
                }
            } catch (Exception ex) {
                return new Response(MessageConstants.MSG_ACCESS_DENY, null, 0, "读取数据错误", request);
            }
        }
    }


    /**
     * 查询数据
     *
     * @param request
     * @param jsonParam
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/SelfBalanceAll", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public Response SelfBalanceAll(HttpServletRequest request, @RequestBody JSONObject jsonParam) {
        Map<String, Object> map = FinaceVer.Security(request, jsonParam);
        if (!map.get("code").toString().equals("000000")) {
            return new Response(MessageConstants.MSG_ACCESS_DENY, null, "接口参数错误", request);
        } else {
            try {
                String res = map.get("parm").toString();
                int AID = Integer.parseInt(map.get("AID").toString());
                System.out.println("AID:" + AID);

                //将接收到的数据转化为对象
                FinaceBalanceAll finaceBalanceAll1 = JSON.parseObject(res, new TypeReference<FinaceBalanceAll>() {
                });

                FinaceBalanceAll finaceBalanceAll = finaceService.SelectBalanceAll(finaceBalanceAll1);

                if (finaceBalanceAll != null) {
                    return new Response(MessageConstants.MSG_SUCCESS, finaceBalanceAll, 1, "", request);
                } else {
                    return new Response(MessageConstants.MSG_ITEM_NOT_EXIST, null, 0, "读取数据错误", request);
                }
            } catch (Exception ex) {
                return new Response(MessageConstants.MSG_ACCESS_DENY, null, 0, "读取数据错误", request);
            }
        }
    }

    /**
     * 查询数据
     *
     * @param request
     * @param jsonParam
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/SelfBalanceAllX", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public Response SelfBalanceAllX(HttpServletRequest request, @RequestBody JSONObject jsonParam) {
        Map<String, Object> map = FinaceVer.Security(request, jsonParam);
        if (!map.get("code").toString().equals("000000")) {
            return new Response(MessageConstants.MSG_ACCESS_DENY, null, "接口参数错误", request);
        } else {
            try {
                String res = map.get("parm").toString();
                int AID = Integer.parseInt(map.get("AID").toString());
                System.out.println("AID:" + AID);
                //将接收到的数据转化为对象
                FinaceBalanceAllX finaceBalanceAllX1 = JSON.parseObject(res, new TypeReference<FinaceBalanceAllX>() {
                });

                FinaceBalanceAllX finaceBalanceAllX = finaceService.SelectBalanceAllX(finaceBalanceAllX1);

                if (finaceBalanceAllX != null) {
                    return new Response(MessageConstants.MSG_SUCCESS, finaceBalanceAllX, 1, "", request);
                } else {
                    return new Response(MessageConstants.MSG_ITEM_NOT_EXIST, null, 0, "读取数据错误", request);
                }
            } catch (Exception ex) {
                return new Response(MessageConstants.MSG_ACCESS_DENY, null, 0, "读取数据错误", request);
            }
        }
    }

    /**
     * 查询数据
     *
     * @param request
     * @param jsonParam
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/SelfBalanceAllJ", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public Response SelfBalanceAllJ(HttpServletRequest request, @RequestBody JSONObject jsonParam) {
        Map<String, Object> map = FinaceVer.Security(request, jsonParam);
        if (!map.get("code").toString().equals("000000")) {
            return new Response(MessageConstants.MSG_ACCESS_DENY, null, "接口参数错误", request);
        } else {
            try {
                String res = map.get("parm").toString();
                int AID = Integer.parseInt(map.get("AID").toString());
                System.out.println("AID:" + AID);
                //将接收到的数据转化为对象
                FinaceBalanceAllJ finaceBalanceAllJ1 = JSON.parseObject(res, new TypeReference<FinaceBalanceAllJ>() {
                });

                FinaceBalanceAllJ finaceBalanceAllJ = finaceService.SelectBalanceAllJ(finaceBalanceAllJ1);

                if (finaceBalanceAllJ != null) {
                    return new Response(MessageConstants.MSG_SUCCESS, finaceBalanceAllJ, 1, "", request);
                } else {
                    return new Response(MessageConstants.MSG_ITEM_NOT_EXIST, null, 0, "读取数据错误", request);
                }
            } catch (Exception ex) {
                return new Response(MessageConstants.MSG_ACCESS_DENY, null, 0, "读取数据错误", request);
            }
        }
    }

    @ResponseBody
    @RequestMapping(value = "/SelectDisId")
    public Response SelectDisId(HttpServletRequest request) {
        Finace finace=finaceService.SelectDisId();
        if(finace!=null) {
            System.out.println("A");
            finaceService.deleteByPrimaryKey(finace.getId(), 1);
        }
        return new Response(MessageConstants.MSG_SUCCESS, finace, 1, "", request);
    }

}